package com.store.app.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.store.app.R;
import com.store.app.bean.BitmapBean;

import java.util.List;

/**
 * Created by Administrator on 2017/5/17 0017.
 */
public class AddGoodsRVAdapter extends RecyclerView.Adapter{

    private Context mContext;
    private List<BitmapBean> mUploadBitmapBeans;

    public AddGoodsRVAdapter(Context context, List<BitmapBean> uploadBitmapBeans) {
        mContext = context;
        mUploadBitmapBeans = uploadBitmapBeans;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_rv_pic_add_goods,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        BitmapBean uploadBitmapBean = mUploadBitmapBeans.get(position);
        initListener(viewHolder,position);
        viewHolder.bindData(uploadBitmapBean);
    }

    private void initListener(ViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnPicItemClickListener != null){
                    mOnPicItemClickListener.onPicItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mUploadBitmapBeans == null ? 0 : mUploadBitmapBeans.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        public ImageView iv_pic;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_pic = (ImageView) itemView.findViewById(R.id.iv_pic);
        }

        public void bindData(BitmapBean uploadBitmapBean) {
//            iv_pic.setImageBitmap(uploadBitmapBean.getBitmap());

            //显示头像
            ImageLoader.getInstance().displayImage(uploadBitmapBean.getDoc_path(), iv_pic, new ImageLoadingListener() {

                @Override
                public void onLoadingStarted(String s, View view) {
                }

                @Override
                public void onLoadingFailed(String s, View view, FailReason failReason) {
                    iv_pic.setBackgroundResource(R.drawable.default_picture_s);
                }

                @Override
                public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                }


                @Override
                public void onLoadingCancelled(String s, View view) {
                    Log.i("mylog", "ff");
                }
            });
        }
    }


    private OnPicItemClickListener mOnPicItemClickListener;

    public void setOnPicItemClickListener(OnPicItemClickListener onPicItemClickListener) {
        mOnPicItemClickListener = onPicItemClickListener;
    }

    /**定义图片条目点击监听*/
    public interface OnPicItemClickListener{
        void onPicItemClick(int position);
    }


}
